//
//  BianKuangBeiJingSeViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/9/13.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

protocol SheZhiLabelYangShiDelegate:class {
    func chuanDiXuanZhongFenZhongZhiAndBeiJingSe(_ xuanZhongDeFenZhongZhi:Int,xuanZhongBeiJingSeByIndexPathDianRow:Int)
}


class BianKuangBeiJingSeViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    //定义代理变量
    weak var delegate:SheZhiLabelYangShiDelegate?

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var navigationHeight:CGFloat?
    
    let layout = UICollectionViewFlowLayout()
    
    var collection:UICollectionView?
    
    //完成按钮
    var wanChengBtn:UIButton?

    //定义被选中的imageView
    var selectedImagView:UIImageView?
    
    //定义一个变量,保存点击的indexPath.row
    var indexPathDianRow:Int = 16
    
    var selectedMiniteValue:Int = 0
    
    override func viewDidLoad() {
        
        let leftBarBtn = UIBarButtonItem(title: "取消", style: .plain, target: self,action: #selector(backToPrevious))
        self.navigationItem.leftBarButtonItem = leftBarBtn
        self.view.backgroundColor = UIColor.white
        self.title = "背景色设置"
        
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        //修改导航栏按钮颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor =
            UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.layer.bounds.height
        
        //初始化右侧的按钮
        wanChengBtn = UIButton(frame:CGRect(x: 0, y: 0, width: 60, height: 30))
        wanChengBtn?.setTitle("完成", for: UIControlState())
        wanChengBtn?.setTitleColor(UIColor.white, for: UIControlState())

        let rightBarButtonItem = UIBarButtonItem(customView: wanChengBtn!)
        self.navigationItem.rightBarButtonItem = rightBarButtonItem
        
        //定义布局
        layout.scrollDirection = UICollectionViewScrollDirection.horizontal  //滚动方向
        layout.itemSize = CGSize(width: 40, height: 40) //设置所有cell的size  太重要了 找了半天。(自学就是辛苦呀！！)
        layout.minimumLineSpacing = 5.0  //上下间隔
        layout.minimumInteritemSpacing = 5.0 //左右间隔
        layout.headerReferenceSize = CGSize(width: 10, height: 10)
        layout.footerReferenceSize = CGSize(width: 10, height: 10)
        
        
        collection = UICollectionView(frame: CGRect(x: 5, y: navigationHeight!+50, width: screenWidth, height: 60),collectionViewLayout:layout)
        collection!.backgroundColor = UIColor.white
        collection?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")
        collection?.contentSize = CGSize(width: screenWidth*4, height: 60)
        collection?.alwaysBounceHorizontal = true
        
        collection!.delegate = self
        collection!.dataSource = self
        print(selectedMiniteValue)//2
        wanChengBtn!.addTarget(self,action:#selector(wanChengSheZhi),for:.touchUpInside)
        self.view.addSubview(wanChengBtn!)
        self.view.addSubview(collection!)
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //设置分区个数
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    //设置每个分区元素个数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  16
    }

    //设置元素内容
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //这里创建cell
        let cellIdentifier = "collectionViewCell"
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
        switch indexPath.row {
        case 0:
            cell.backgroundColor = UIColor(red: 255/255,green: 130/255,blue: 171/255,alpha: 1)
        case 1:
            cell.backgroundColor = UIColor(red: 255/255,green: 174/255,blue: 185/255,alpha: 1)
        case 2:
            cell.backgroundColor = UIColor(red: 255/255,green: 218/255,blue: 185/255,alpha: 1)
        case 3:
            cell.backgroundColor = UIColor(red: 255/255,green: 106/255,blue: 106/255,alpha: 1)
        case 4:
            cell.backgroundColor = UIColor(red: 238/255,green: 216/255,blue: 174/255,alpha: 1)
        case 5:
            cell.backgroundColor = UIColor(red: 220/255,green: 220/255,blue: 220/255,alpha: 1)
        case 6:
            cell.backgroundColor = UIColor(red: 72/255,green: 209/255,blue: 204/255,alpha: 1)
        case 7:
            cell.backgroundColor = UIColor(red: 124/255,green: 252/255,blue: 0/255,alpha: 1)
        case 8:
            cell.backgroundColor = UIColor(red: 189/255,green: 183/255,blue: 107/255,alpha: 1)
        case 9:
            cell.backgroundColor = UIColor(red: 244/255,green: 164/255,blue: 96/255,alpha: 1)
        case 10:
            cell.backgroundColor = UIColor(red: 240/255,green: 128/255,blue: 128/255,alpha: 1)
        case 11:
            cell.backgroundColor = UIColor(red: 188/255,green: 143/255,blue: 143/255,alpha: 1)
        case 12:
            cell.backgroundColor = UIColor(red: 143/255,green: 188/255,blue: 143/255,alpha: 1)
        case 13:
            cell.backgroundColor = UIColor(red: 64/255,green: 224/255,blue: 208/255,alpha: 1)
        case 14:
            cell.backgroundColor = UIColor(red: 0/255,green: 139/255,blue: 139/255,alpha: 1)
        case 15:
            cell.backgroundColor = UIColor(red: 34/255,green: 139/255,blue: 34/255,alpha: 1)
            
        default:
            print("其它颜色")
        }
        
        cell.contentView.backgroundColor = UIColor.gray
        cell.contentView.alpha = 0.2
        return cell
    }

    //点击item时触发
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        indexPathDianRow = indexPath.row
        print(indexPathDianRow)
    }
    
    //是否显示高亮效果
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
        
    }
    
    //当点击完成的时候,实现对主页面背景色的设置
    func wanChengSheZhi(){
        switch indexPathDianRow {
        case 0:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 1:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 2:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 3:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 4:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 5:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 6:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 7:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
            
        case 8:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 9:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 10:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 11:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 12:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 13:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 14:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        case 15:
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
            
        default:
            print("其它颜色")
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndBeiJingSe(selectedMiniteValue, xuanZhongBeiJingSeByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)

        }
    }


    //当点击某个cell的时候显示高亮颜色
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell:UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
        
        //初始化imageView对象
        selectedImagView = UIImageView(frame:CGRect(x: 5, y: navigationHeight!+10, width: 10, height: 10))
        selectedImagView?.image = UIImage(named: "checkON")
        
        //初始化UIView对象
        let view:UIView = UIView(frame:CGRect(x: 5,y: navigationHeight!+10,width: 5,height: 5))
        view.layer.cornerRadius = 2
        view.layer.contents = selectedImagView?.image?.cgImage
        view.addSubview(selectedImagView!)
        cell.selectedBackgroundView = view
        
    }

    
    //返回至上一个界面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
